package com.activity.gaswatchermtl.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import com.activity.gaswatchermtl.R;
import com.inf8405.db.DAOStations;
import com.module.StationFinder.Station;
import com.module.StationFinder.StationList;
import com.module.StationFinder.AsyncTask.LoadStationDetailTask;
import com.module.helper.AlertDialogHelper;
import com.activity.gaswatchermtl.StationDetailsActivity;
import com.app.Const;

import android.content.Intent;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class FragmentTabList extends Fragment {

	private StationList stationList_ = null;
	private ArrayList<HashMap<String, String>> list4Adapter_ = null;

	private View inflatedView_ = null;
	private ListView listView_ = null;


	@Override
	public void onCreate (Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		if (inflatedView_ == null){
			inflatedView_ = inflater.inflate(R.layout.fragment_station_list, container, false);	
		}
		listView_ = (ListView) inflatedView_.findViewById(R.id.list_tab);
		
		if (listView_ != null) {
			listView_.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {
					// getting values from selected ListItem
					String reference = ((TextView) view.findViewById(R.id.reference)).getText().toString();

					// Starting new intent
					Intent in = new Intent(getActivity().getApplicationContext(), StationDetailsActivity.class);

					// Sending place refrence id to single place activity
					// place refrence id used to get "Place full details"
					in.putExtra(StationDetailsActivity.KEY_REFERENCE, reference);
					startActivity(in);
				}
			});
		}
		return inflatedView_;
	}

	public void refreshStationList(StationList listStation) {
		stationList_ = listStation;
		String status = stationList_.status;
		if(status.equals("OK")){
			// Successfully got places details
			if (stationList_.results != null) {
				// loop through each place
				list4Adapter_ = new ArrayList<HashMap<String,String>>();
				for (Station station : stationList_.results) {
					HashMap<String, String> map = new HashMap<String, String>();

					// Place reference won't display in listview - it will be hidden
					// Place reference is used to get "place full details"
					map.put(StationDetailsActivity.KEY_REFERENCE, station.reference);
					// Place name
					map.put(StationDetailsActivity.KEY_NAME, station.name);
					//map.put(StationDetailsActivity.KEY_PRICE, "111");

					// adding HashMap to ArrayList
					list4Adapter_.add(map);
					
				}
			}
		} else {
			showError(status);
		}

		// Remplir la liste avec les stations trouver
		if (list4Adapter_ != null && listView_ != null)
		{
			// list adapter
			ListAdapter adapter = new SimpleAdapter(inflatedView_.getContext(), list4Adapter_,
					R.layout.list_item,
					new String[] { StationDetailsActivity.KEY_REFERENCE, StationDetailsActivity.KEY_NAME }, new int[] {
				R.id.reference, R.id.name });

			// Adding data into listview
			listView_.setAdapter(adapter);
		}
	}
	
	private void showError(String status){
		// Analyse de l'erreur
		if(status.equals("ZERO_RESULTS")) {
			// Zero results found
			AlertDialogHelper.showAlertDialog(getActivity(), "Near Places",
					"Sorry no places found. Try to change the types of places",
					null);
		}
		else if(status.equals("UNKNOWN_ERROR"))
		{
			AlertDialogHelper.showAlertDialog(getActivity(), "Places Error",
					"Sorry unknown error occured.",
					null);
		}
		else if(status.equals("OVER_QUERY_LIMIT"))
		{
			AlertDialogHelper.showAlertDialog(getActivity(), "Places Error",
					"Sorry query limit to google places is reached",
					null);
		}
		else if(status.equals("REQUEST_DENIED"))
		{
			AlertDialogHelper.showAlertDialog(getActivity(), "Places Error",
					"Sorry error occured. Request is denied",
					null);
		}
		else if(status.equals("INVALID_REQUEST"))
		{
			AlertDialogHelper.showAlertDialog(getActivity(), "Places Error",
					"Sorry error occured. Invalid Request",
					null);
		}
		else
		{
			AlertDialogHelper.showAlertDialog(getActivity(), "Places Error",
					"Sorry error occured.",
					null);
		}		
	}

}